<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工厂">
          <el-select
            @change="plantChange(searchParam.plantCode)"
            v-model="searchParam.plantCode"
            placeholder="请选择工厂"
          >
            <el-option
              v-for="item in plants"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.organizationCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心">
          <el-select
            v-model="searchParam.workCenterCode"
            clearable
            placeholder="请选择工作中心"
          >
            <el-option
              v-for="item in workshops"
              :key="item.id"
              :label="item.workshopCode"
              :value="item.workshopCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="计划周数">
          <el-input
            v-model="searchParam.planWeek"
            placeholder="请输入计划周数"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="QAD加工单号">
          <el-input
            v-model="searchParam.workOrderCode"
            placeholder="请输入QAD加工单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!-- <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
        <el-button
          class="ml-3"
          @click="useDownload('/mes/mesworkorder/export', '工单', searchParam)"
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template> -->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" type="index" width="55" align="center" />
        <el-table-column
          label="QAD加工单号"
          prop="workOrderCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="生产计划号"
          prop="producePlanCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工厂"
          prop="plantCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="物料号"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划周数"
          prop="planWeek"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column label="计划状态" prop="orderState" min-width="100">
          <template #default="scope">
            {{ useDictionary("ProduceOrderState", scope.row.orderState) }}
          </template>
        </el-table-column>
        <el-table-column
          label="计划数量"
          prop="planQuantity"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="完工数量"
          prop="planFinishQuantity"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="是否启用"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">是</el-tag>
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesworkorder-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESWORKORDER",
  inheritAttrs: false,
});

let plants = ref([]);
let workshops = ref([]);
// 请求参数
const requestApi = "/mes/mesworkorder/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  workOrderCode: "",
  plantCode: "",
  workCenterCode: "",
  planWeek: "",
};

const deleteParam = {
  api: "/mes/mesworkorder/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlants();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesworkorder/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
}
async function getWorkshops(plantcode: any) {
  const data = await request({
    url: "/mes/mesworkshop/getList",
    method: "post",
    data: { plantCode: plantcode },
  });
  workshops.value = data.data;
}
function plantChange(plantcode: any) {
  var plant = plants.value.find((item) => {
    return item.organizationCode === plantcode;
  });
  getWorkshops(plantcode);
}
/** 打开工单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改工单";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增工单";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开工单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "工单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
